/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.sdawd
 */
package Client;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import ui.Main;

import Games.GamesController;
import Games.TicTacToeComputerAi;
import Games.ComputerPlayer;

/**
 *
 * @author Brandon
 */
public class CpuFunctions extends Thread {
    
	private String playerOne;
	private String playerTwo;
	
    public CpuFunctions() {
    }
    
    @Override
    public void run() {
        while(Comm.connected) {
            try {
                synchronized(ComputerPlayer.comm.queue) {
                    if(ComputerPlayer.comm.queue.isEmpty())
                    	ComputerPlayer.comm.queue.wait();
                    execute(ComputerPlayer.comm.queue.poll());
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(CpuFunctions.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void execute(HashMap<String, String[]> function) {
        if(function != null) {
        	if(function.containsKey("LOGIN"))
        	{
        		String[] values = function.get("LOGIN");
        		if(values[0] == "OK"){
     			
        		}else{

        		}
        		
        	}else if(function.containsKey("GAMELIST")) {
        		String[] games =  function.get("GAMELIST");
        		for(int i = 0; i < games.length; i++ ){
        			//Main.addServerGames(games[i], i);
        		}

            } else if(function.containsKey("MOVE")) {
                //Receive a move
                String[] details = function.get("MOVE");

            }else if(function.containsKey("YOURTURN")){
            	
            	while(!Main.received){
	            	try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
					}
            	}
            	ComputerPlayer.ComputerAi.chooseMove(GamesController.gamesModel().getPlayersHashMap().get(GamesController.gamesModel().getThem()));

            }else if(function.containsKey("MATCH")){
                //Accept a match
                String[] details = function.get("MATCH");
                
            }else if(function.containsKey("WIN")){
            		
            }else if(function.containsKey("LOSS")){

            }else if(function.containsKey("DRAW")){

            }else if(function.containsKey("ERR") && Command.getLastCommand().equals("login")){

            } else
                //throw new UnsupportedOperationException("This function is not yet implemented");
                    System.out.println(function.toString());
        }
    }

	public String getPlayerOne() {
		return playerOne;
	}

	public void setPlayerOne(String playerOne) {
		this.playerOne = playerOne;
	}

	public String getPlayerTwo() {
		return playerTwo;
	}

	public void setPlayerTwo(String playerTwo) {
		this.playerTwo = playerTwo;
	}
}
